#include "dlgmain.h"
#include "ui_dlgmain.h"
#include "tmglobalctrl.h"

DlgMain::DlgMain(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgMain)
{
    ui->setupUi(this);

    pr_setting = NULL;

    init();

}

DlgMain::~DlgMain()
{
    delete ui;
}

void DlgMain::init()
{
    QString g_strRootDir = QCoreApplication::applicationDirPath();
    QDir::setCurrent(g_strRootDir);

    ui->tabWidget->setCurrentIndex(0);
    ui->tabWidget->tabBar()->hide();
    ui->pushButton_json->setChecked(true);
    for(int i = 0;i < 50;i++){
        ui->comboBox_B->addItem(QString("%1").arg(i));
        ui->comboBox_F->addItem(QString("%1").arg(i));
    }
    //json
    QThread* pr_jsonThread = new QThread;
    pr_JsonWorker = new TMJsonWorker();
    pr_JsonWorker->moveToThread(pr_jsonThread);
    pr_jsonThread->start();
    jsonConnect();
    //motor
    QThread* pr_motorThread = new QThread;
    pr_MotorWorker = new TMMotorWorker();
    pr_MotorWorker->moveToThread(pr_motorThread);
    pr_motorThread->start();
    motorConnect();
    //audio
    QThread* pr_audioThread = new QThread;
    pr_AudioWorker = new TMAudioWorker();
    pr_AudioWorker->moveToThread(pr_audioThread);
    pr_audioThread->start();
    audioConnect();
}

void DlgMain::jsonConnect()
{
    connect(this,SIGNAL(sig_loadJsonFile(QString)),pr_JsonWorker,SLOT(slot_loadJsonFile(QString)));
    connect(this,SIGNAL(sig_loadJsonByName(QString)),pr_JsonWorker,SLOT(slot_loadJsonByName(QString)));
    connect(this,SIGNAL(sig_loadJsonByContent(QString)),pr_JsonWorker,SLOT(slot_loadJsonByContent(QString)));
    connect(this,SIGNAL(sig_loadJsonByClass(QString)),pr_JsonWorker,SLOT(slot_loadJsonByClass(QString)));
    connect(this,SIGNAL(sig_getJsonList()),pr_JsonWorker,SLOT(slot_getJsonList()));
    connect(this,SIGNAL(sig_getJsonState()),pr_JsonWorker,SLOT(slot_getJsonState()));
    connect(this,SIGNAL(sig_stop()),pr_JsonWorker,SLOT(slot_stop()));
    connect(pr_JsonWorker,SIGNAL(sig_jsonList(QStringList)),this,SLOT(slot_getJsonList(QStringList)));
    connect(pr_JsonWorker,SIGNAL(sig_jsonState(bool)),this,SLOT(slot_getJsonState(bool)));
}

void DlgMain::motorConnect()
{
    //head
    connect(this,SIGNAL(sig_headEnable()),pr_MotorWorker,SLOT(slot_headEnable()));
    connect(this,SIGNAL(sig_headPowerDown()),pr_MotorWorker,SLOT(slot_headPowerDown()));
    connect(this,SIGNAL(sig_headLRInit()),pr_MotorWorker,SLOT(slot_headLRInit()));
    connect(this,SIGNAL(sig_headLRAngle(int,int)),pr_MotorWorker,SLOT(slot_headLRAngle(int,int)));
    connect(this,SIGNAL(sig_headUDInit()),pr_MotorWorker,SLOT(slot_headUDInit()));
    connect(this,SIGNAL(sig_headUDAngle(int,int)),pr_MotorWorker,SLOT(slot_headUDAngle(int,int)));
    //left hand
    connect(this,SIGNAL(sig_handLEnable()),pr_MotorWorker,SLOT(slot_handLEnable()));
    connect(this,SIGNAL(sig_handLPowerDown()),pr_MotorWorker,SLOT(slot_handLPowerDown()));
    connect(this,SIGNAL(sig_handLExpandInit()),pr_MotorWorker,SLOT(slot_handLExpandInit()));
    connect(this,SIGNAL(sig_handLExpandAngle(int,int)),pr_MotorWorker,SLOT(slot_handLExpandAngle(int,int)));
    connect(this,SIGNAL(sig_handLSwingInit()),pr_MotorWorker,SLOT(slot_handLSwingInit()));
    connect(this,SIGNAL(sig_handLSwingAngle(int,int)),pr_MotorWorker,SLOT(slot_handLSwingAngle(int,int)));
    //right hand
    connect(this,SIGNAL(sig_handREnable()),pr_MotorWorker,SLOT(slot_handREnable()));
    connect(this,SIGNAL(sig_handRPowerDown()),pr_MotorWorker,SLOT(slot_handRPowerDown()));
    connect(this,SIGNAL(sig_handRExpandInit()),pr_MotorWorker,SLOT(slot_handRExpandInit()));
    connect(this,SIGNAL(sig_handRExpandAngle(int,int)),pr_MotorWorker,SLOT(slot_handRExpandAngle(int,int)));
    connect(this,SIGNAL(sig_handRSwingInit()),pr_MotorWorker,SLOT(slot_handRSwingInit()));
    connect(this,SIGNAL(sig_handRSwingAngle(int,int)),pr_MotorWorker,SLOT(slot_handRSwingAngle(int,int)));
    //wheel
    connect(this,SIGNAL(sig_wheelEnable()),pr_MotorWorker,SLOT(slot_wheelEnable()));
    connect(this,SIGNAL(sig_wheelPowerDown()),pr_MotorWorker,SLOT(slot_wheelPowerDown()));
    connect(this,SIGNAL(sig_wheelStop(bool)),pr_MotorWorker,SLOT(slot_wheelStop(bool)));
    connect(this,SIGNAL(sig_wheelRun(int,int)),pr_MotorWorker,SLOT(slot_wheelRun(int,int)));
    connect(this,SIGNAL(sig_wheelRunDiff(bool,int,bool,int)),pr_MotorWorker,SLOT(slot_wheelRunDiff(bool,int,bool,int)));
    connect(this,SIGNAL(sig_wheelRunRotate(int,int)),pr_MotorWorker,SLOT(slot_wheelRunRotate(int,int)));
    //sensor
    connect(this,SIGNAL(sig_sensorBattery()),pr_MotorWorker,SLOT(slot_sensorBattery()));
    connect(this,SIGNAL(sig_sensorInfrared()),pr_MotorWorker,SLOT(slot_sensorInfrared()));
    connect(this,SIGNAL(sig_sensorSonic()),pr_MotorWorker,SLOT(slot_sensorSonic()));
    connect(this,SIGNAL(sig_sensorWheel()),pr_MotorWorker,SLOT(slot_sensorWheel()));
    connect(pr_MotorWorker,SIGNAL(sig_sensorBatteryInfo(QString)),this,SLOT(slot_sensorBatteryInfo(QString)));
    connect(pr_MotorWorker,SIGNAL(sig_sensorInfraredInfo(QString)),this,SLOT(slot_sensorInfraredInfo(QString)));
    connect(pr_MotorWorker,SIGNAL(sig_sensorSonicInfo(QString)),this,SLOT(slot_sensorSonicInfo(QString)));
    connect(pr_MotorWorker,SIGNAL(sig_sensorWheelInfo(QString)),this,SLOT(slot_sensorWheelInfo(QString)));
    //led
    connect(this,SIGNAL(sig_ledBodyLed(int,bool,bool,bool)),pr_MotorWorker,SLOT(slot_ledBodyLed(int,bool,bool,bool)));
    connect(this,SIGNAL(sig_ledHeadEar(int,bool,bool,bool)),pr_MotorWorker,SLOT(slot_ledHeadEar(int,bool,bool,bool)));
    connect(this,SIGNAL(sig_ledHeadEye(int,int)),pr_MotorWorker,SLOT(slot_ledHeadEye(int,int)));
    connect(this,SIGNAL(sig_ledHeadEye2(int,bool,bool,bool)),pr_MotorWorker,SLOT(slot_ledHeadEye2(int,bool,bool,bool)));
    connect(this,SIGNAL(sig_ledTouchSet(bool,int,int)),pr_MotorWorker,SLOT(slot_ledTouchSet(bool,int,int)));
    //壁障
    connect(this,SIGNAL(sig_protectSet(bool)),pr_MotorWorker,SLOT(slot_protectSet(bool)));
    connect(this,SIGNAL(sig_protectState()),pr_MotorWorker,SLOT(slot_protectState()));
    connect(pr_MotorWorker,SIGNAL(sig_getProtectState(bool)),this,SLOT(slot_getProtectState(bool)));
}

void DlgMain::audioConnect()
{
    connect(this,SIGNAL(sig_ttsPlay(QString)),pr_AudioWorker,SLOT(slot_ttsPlay(QString)));
    connect(this,SIGNAL(sig_ttsPlayParams(QString)),pr_AudioWorker,SLOT(slot_ttsPlayParams(QString)));
    connect(this,SIGNAL(sig_ttsPlayFile(QString)),pr_AudioWorker,SLOT(slot_ttsPlayFile(QString)));
    connect(this,SIGNAL(sig_ttsSetVolume(int)),pr_AudioWorker,SLOT(slot_ttsSetVolume(int)));
    connect(this,SIGNAL(sig_ttsStop()),pr_AudioWorker,SLOT(slot_ttsStop()));
    connect(pr_AudioWorker,SIGNAL(sig_ttsState(bool)),this,SLOT(slot_ttsState(bool)));

    connect(this,SIGNAL(sig_asrBuild(QString)),pr_AudioWorker,SLOT(slot_asrBuild(QString)));
    connect(this,SIGNAL(sig_asrStart(int)),pr_AudioWorker,SLOT(slot_asrStart(int)));
    connect(this,SIGNAL(sig_asrStop()),pr_AudioWorker,SLOT(slot_asrStop()));

    connect(this,SIGNAL(sig_mediaPlay(QString)),pr_AudioWorker,SLOT(slot_mediaPlay(QString)));
    connect(this,SIGNAL(sig_mediaStop()),pr_AudioWorker,SLOT(slot_mediaStop()));
    connect(this,SIGNAL(sig_mediaVolume(int)),pr_AudioWorker,SLOT(slot_mediaVolume(int)));
    connect(pr_AudioWorker,SIGNAL(sig_mediaState(bool)),this,SLOT(slot_getMediaState(bool)));
}

void DlgMain::on_pushButton_conn_clicked()
{
    m_ip = ui->lineEdit_ip->text();
//    QString m_Locator = QString("--Ice.Default.Locator=RobotIceGrid/Locator:tcp -h %1 -p 4061").arg(m_ip);
//    QString m_TopicManager = QString("--TopicManager.Proxy=PubCenter/TopicManager");

//     Ice::StringSeq argList;
//     argList.push_back(m_Locator.toUtf8().toStdString());
//     argList.push_back(m_TopicManager.toUtf8().toStdString());//无效，未绑定上
//     m_icePtr = Ice::initialize(argList);

    Ice::InitializationData iniData;
    iniData.properties = Ice::createProperties();
    iniData.properties->setProperty("Ice.Default.Locator", QString("RobotIceGrid/Locator:tcp -h %1 -p 4061").arg(m_ip).toUtf8().toStdString());
    iniData.properties->setProperty("TopicManager.Proxy", "PubCenter/TopicManager");
    m_icePtr = Ice::initialize(iniData);
    
    try
    {
        m_prxJson = Robot::JsonPrx::checkedCast(m_icePtr->stringToProxy("Json"));
        m_prxMotor = Robot::MotorPrx::checkedCast(m_icePtr->stringToProxy("Motor"));
        m_prxAudio = Robot::AudioPrx::checkedCast(m_icePtr->stringToProxy("Audio"));
        //        m_prxScreenEx = Robot::ScreenExPrx::checkedCast(m_icePtr->stringToProxy("ScreenEx"));
        m_prxConfig = Robot::ConfigPrx::checkedCast(m_icePtr->stringToProxy("Config"));
        m_prxPub = Robot::PubPrx::checkedCast(m_icePtr->stringToProxy("Pub"));
        m_prxSession = Robot::SessionPrx::checkedCast(m_icePtr->stringToProxy("Session"));
        m_prxLive = Robot::LivePrx::checkedCast(m_icePtr->stringToProxy("Live"));

        pr_JsonWorker->m_prTimer->start(500);
        pr_AudioWorker->pr_Timer->start(500);
        try{
            int nVolume = m_prxConfig->GetSystemVolume();
            ui->horizontalSlider->setValue(nVolume);
            ui->label_systemVolume->setText(QString("%1").arg(nVolume));
        }catch(Ice::Exception& e)
        {
            qDebug()<<"Error:"<<e.what();
        }
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
        QMessageBox::warning(this, tr("ERROR"),e.what(),QMessageBox::Ok);
        return;
    }
    //    qDebug()<<m_prxMotor->ice_toString().c_str()<<m_prxAudio->ice_toString().c_str();
}

void DlgMain::on_pushButton_json_clicked()
{
    ui->tabWidget->setCurrentIndex(0);
}

void DlgMain::on_pushButton_motor_clicked()
{
    ui->tabWidget->setCurrentIndex(1);
}

void DlgMain::on_pushButton_audio_clicked()
{
    ui->tabWidget->setCurrentIndex(2);
}

void DlgMain::on_pushButton_screenEx_clicked()
{
    ui->tabWidget->setCurrentIndex(3);
}

void DlgMain::on_pushButton_config_clicked()
{
    ui->tabWidget->setCurrentIndex(4);
}

void DlgMain::on_pushButton_pub_clicked()
{
    ui->tabWidget->setCurrentIndex(5);
}

void DlgMain::on_pushButton_session_clicked()
{
    ui->tabWidget->setCurrentIndex(6);
}

void DlgMain::on_pushButton_live_clicked()
{
    ui->tabWidget->setCurrentIndex(7);
}
//==============json=================================//
void DlgMain::on_pushButton_name_clicked()
{
    QString m_strName = ui->lineEdit_name->text();

    emit sig_loadJsonByName(m_strName);
}

void DlgMain::on_pushButton_class_clicked()
{
    QString m_strClass = ui->lineEdit_name->text();
    emit sig_loadJsonByClass(m_strClass);

}

void DlgMain::on_pushButton_content_clicked()
{
    QString m_strContent = ui->textEdit_content->toPlainText();

    emit sig_loadJsonByContent(m_strContent);
}

void DlgMain::on_pushButton_load_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(NULL, tr("Open File"),"/",tr("Files (*.json)"));
    emit sig_loadJsonFile(filePath);
}

void DlgMain::on_pushButton_list_clicked()
{
    emit sig_getJsonList();
}

void DlgMain::on_pushButton_stop_clicked()
{
    emit sig_stop();
}

void DlgMain::slot_getJsonState(bool state)
{
    if(state){
        ui->label_state->setText("Playing");
    }else{
        ui->label_state->setText("Stopped");
    }
}

void DlgMain::slot_getJsonList(QStringList m_list)
{
    for(int i = 0;i < m_list.size();i++)
        ui->textEdit_list->append(m_list.at(i));
}

//===============Motor====================//
//head
void DlgMain::on_Btn_headEnable_clicked()
{
    emit sig_headEnable();
}

void DlgMain::on_Btn_headPowerDown_clicked()
{
    emit sig_headPowerDown();
}

void DlgMain::on_Btn_headLRInit_clicked()
{
    emit sig_headLRInit();
}

void DlgMain::on_Btn_headUDInit_clicked()
{
    emit sig_headUDInit();
}

void DlgMain::on_Btn_headLRAngle_clicked()
{
    int nAngle = ui->headLRAngleEdit->text().toInt();
    int nSpeed = ui->headLRSpeedEdit->text().toInt();
    emit sig_headLRAngle(nAngle,nSpeed);
}

void DlgMain::on_Btn_headUDAngle_clicked()
{
    int nAngle = ui->headUDAngleEdit->text().toInt();
    int nSpeed = ui->headUDSpeedEdit->text().toInt();
    emit sig_headUDAngle(nAngle,nSpeed);
}
//left hand
void DlgMain::on_Btn_handLEnable_clicked()
{
    emit sig_handLEnable();
}

void DlgMain::on_Btn_handLPowerDown_clicked()
{
    emit sig_handLPowerDown();
}

void DlgMain::on_Btn_handLSwingInit_clicked()
{
    emit sig_handLSwingInit();
}

void DlgMain::on_Btn_handLExpandInit_clicked()
{
    emit sig_handLExpandInit();
}

void DlgMain::on_Btn_handLSwingAngle_clicked()
{
    int nAngle = ui->handLSwingAngleEdit->text().toInt();
    int nSpeed = ui->handLSwingSpeedEdit->text().toInt();
    emit sig_handLSwingAngle(nAngle,nSpeed);
}

void DlgMain::on_Btn_handLExpandAngle_clicked()
{
    int nAngle = ui->handLExpandAngleEdit->text().toInt();
    int nSpeed = ui->handLExpandSpeedEdit->text().toInt();
    emit sig_handLExpandAngle(nAngle,nSpeed);
}

//right hand
void DlgMain::on_Btn_handREnable_clicked()
{
    emit sig_handREnable();
}

void DlgMain::on_Btn_handRPowerDown_clicked()
{
    emit sig_handRPowerDown();
}

void DlgMain::on_Btn_handRSwingInit_clicked()
{
    emit sig_handRSwingInit();
}

void DlgMain::on_Btn_handRExpandInit_clicked()
{
    emit sig_handRExpandInit();
}

void DlgMain::on_Btn_handRSwingAngle_clicked()
{
    int nAngle = ui->handRSwingAngleEdit->text().toInt();
    int nSpeed = ui->handRSwingSpeedEdit->text().toInt();
    emit sig_handRSwingAngle(nAngle,nSpeed);
}

void DlgMain::on_Btn_handRExpandAngle_clicked()
{
    int nAngle = ui->handRExpandAngleEdit->text().toInt();
    int nSpeed = ui->handRExpandSpeedEdit->text().toInt();
    emit sig_handRExpandAngle(nAngle,nSpeed);
}
//wheel
void DlgMain::on_Btn_wheelEnable_clicked()
{
    emit sig_wheelEnable();
}

void DlgMain::on_Btn_wheelPowerDown_clicked()
{
    emit sig_wheelPowerDown();
}

void DlgMain::on_Btn_wheelStop_clicked()
{
    bool bStopAbrupt  = ui->checkBox_stop->isChecked();
    emit sig_wheelStop(bStopAbrupt);
}

void DlgMain::on_Btn_runRotate_clicked()
{
    int nAngle = ui->wheelRotateAngleEdit->text().toInt();
    int nSpeed = ui->wheelRotateSpeedEdit->text().toInt();
    emit sig_wheelRunRotate(nAngle,nSpeed);
}

void DlgMain::on_Btn_run_clicked()
{
    int nDistance = ui->wheelRunDistanceEdit->text().toInt();
    int nSpeed = ui->wheelRunSpeedEdit->text().toInt();
    emit sig_wheelRun(nDistance,nSpeed);
}

void DlgMain::on_Btn_runDiff_clicked()
{
    bool bLeft = ui->checkBox_left->isChecked();
    int nLeftSpeed = ui->leftSpeedEdit->text().toInt();
    bool bRight = ui->checkBox_right->isChecked();
    int nRightSpeed = ui->rightSpeedEdit->text().toInt();
    emit sig_wheelRunDiff(bLeft,nLeftSpeed,bRight,nRightSpeed);
}
//sensor
void DlgMain::on_Btn_battery_clicked()
{
    emit sig_sensorBattery();//电池信息
}

void DlgMain::on_Btn_sensorSonic_clicked()
{
    emit sig_sensorSonic();//超声波传感器数据
}

void DlgMain::on_Btn_sensorInfrared_clicked()
{
    emit sig_sensorInfrared();//红外传感器数据
}

void DlgMain::on_Btn_sensorWheel_clicked()
{
    emit sig_sensorWheel();//码盘数据
}

void DlgMain::slot_sensorBatteryInfo(QString m_data)
{
    ui->batteryValueEdit->setText(m_data);
}

void DlgMain::slot_sensorSonicInfo(QString m_data)
{
    ui->sonicValueEdit->setText(m_data);
}

void DlgMain::slot_sensorInfraredInfo(QString m_data)
{
    ui->infraredValueEdit->setText(m_data);
}

void DlgMain::slot_sensorWheelInfo(QString m_data)
{
    ui->wheelValueEdit->append(m_data);
}
//LED
void DlgMain::on_Btn_headEye_clicked()
{
    int nIndexF = ui->comboBox_F->currentIndex();
    int nIndexB = ui->comboBox_B->currentIndex();
    emit sig_ledHeadEye(nIndexF,nIndexB);
}

void DlgMain::on_Btn_headEye2_clicked()
{
    int nDelay = ui->eyeLedDelayEdit->text().toInt();
    bool nR = ui->eyeLedREdit->text().toInt();
    bool nG = ui->eyeLedGEdit->text().toInt();
    bool nB = ui->eyeLedBEdit->text().toInt();
    emit sig_ledHeadEye2(nDelay,nR,nG,nB);
}

void DlgMain::on_Btn_headEar_clicked()
{
    int nDelay = ui->earLedDelayEdit->text().toInt();
    bool nR = ui->earLedREdit->text().toInt();
    bool nG = ui->earLedGEdit->text().toInt();
    bool nB = ui->earLedBEdit->text().toInt();
    emit sig_ledHeadEar(nDelay,nR,nG,nB);
}

void DlgMain::on_Btn_bodyLed_clicked()
{
    int nDelay = ui->bodyLedDelayEdit->text().toInt();
    bool nR = ui->bodyLedREdit->text().toInt();
    bool nG = ui->bodyLedGEdit->text().toInt();
    bool nB = ui->bodyLedBEdit->text().toInt();
    emit sig_ledBodyLed(nDelay,nR,nG,nB);
}

void DlgMain::on_Btn_touchSet_clicked()
{
    bool bMode = ui->touchModeEdit->text().toInt();
    int nColor = ui->touchColorEdit->text().toInt();
    int nDelay = ui->touchDelayEdit->text().toInt();
    emit sig_ledTouchSet(bMode,nColor,nDelay);
}
//壁障功能
void DlgMain::on_Btn_protectSet_clicked()
{
    bool bOpen = ui->checkBox_protect->isChecked();
    emit sig_protectSet(bOpen);
}

void DlgMain::on_Btn_protectState_clicked()
{
    emit sig_protectState();
}

void DlgMain::slot_getProtectState(bool state)
{
    if(state){
        ui->label_protect->setText("open");
    }else{
        ui->label_protect->setText("closed");
    }
}
//===================audio===========================
void DlgMain::on_Btn_ttsPlay_clicked()
{
    QString str_tts = ui->lineEdit_tts->text();
    emit sig_ttsPlay(str_tts);
}

void DlgMain::on_Btn_ttsPlayParams_clicked()
{
    QString str_tts = ui->lineEdit_tts->text();
    emit sig_ttsPlayParams(str_tts);
}

void DlgMain::on_Btn_ttsPlayFile_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(NULL, tr("Open File"),"/",tr("TTS Files (*.*)"));
    emit sig_ttsPlayFile(filePath);
}

void DlgMain::on_Btn_ttsSetVolume_clicked()
{
    int nVolume = ui->lineEdit_ttsVolume->text().toInt();
    emit sig_ttsSetVolume(nVolume);
}

void DlgMain::on_Btn_ttsStop_clicked()
{
    emit sig_ttsStop();
}

void DlgMain::slot_ttsState(bool m_bPlaying)
{
    if(m_bPlaying){
        ui->label_ttsState->setText("playing");
    }else{
        ui->label_ttsState->setText("stopped");
    }
}

void DlgMain::on_Btn_asrBulid_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(NULL, tr("Open File"),"/",tr("Files (*.txt)"));
    emit sig_asrBuild(filePath);
}

void DlgMain::on_Btn_asrStart_clicked()
{
    int nMaxTime = ui->lineEdit_asrTime->text().toInt();
    emit sig_asrStart(nMaxTime);
}

void DlgMain::on_Btn_asrStop_clicked()
{
    emit sig_asrStop();
}

void DlgMain::on_Btn_mediaPlay_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(NULL, tr("Open File"),"/",tr("Files (*.*)"));
    emit sig_mediaPlay(filePath);
}

void DlgMain::on_Btn_mediaSetVolume_clicked()
{
    int nVolume = ui->lineEdit_mediaVolume->text().toInt();
    emit sig_mediaVolume(nVolume);
}

void DlgMain::on_Btn_mediaStop_clicked()
{
    emit sig_mediaStop();
}

void DlgMain::slot_getMediaState(bool state)
{
    if(state){
        ui->label_mediaState->setText("playing");
    }else{
        ui->label_mediaState->setText("stopped");
    }
}
//system
void DlgMain::on_Btn_setSystemVolume_clicked()
{
//    int nVolume = ui->lineEdit_volume->text().toInt();
    int nVolume = ui->horizontalSlider->value();
    try{
        m_prxConfig->SetSystemVolume(nVolume);
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void DlgMain::on_Btn_getSystemVolume_clicked()
{
    try{
        int nVolume = m_prxConfig->GetSystemVolume();
        ui->label_systemVolume->setText(QString("%1").arg(nVolume));
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void DlgMain::on_Btn_setSystemCmdVolume_clicked()
{
    QString strCmd = ui->lineEdit_cmd->text();
    try{
        m_prxConfig->SetSystemCmd(strCmd.toUtf8().toStdString());
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}
class PubCenterI : virtual public Robot::Pub {
    // PubCenter interface
public:
    void Notify(const std::string &category, const std::string &msg, const Ice::Current &)
    {
        qDebug()<<"Notify:"<<category.c_str()<<msg.c_str();
    }
};

void DlgMain::on_Btn_pub_clicked()
{
    try
    {
//        Ice::ObjectPrx obj = m_icePtr->stringToProxy("PubCenter/TopicManager"); // 修改这里
        Ice::ObjectPrx obj = m_icePtr->propertyToProxy("TopicManager.Proxy");
        qDebug()<<obj<<m_icePtr;
        m_topicManager = IceStorm::TopicManagerPrx::checkedCast(obj);
        qDebug()<<m_topicManager->ice_toString().c_str();
        Ice::ObjectAdapterPtr adapter = m_icePtr->createObjectAdapterWithEndpoints("MonitorAdapter", "tcp");
        qDebug()<<adapter->getName().c_str();
        Robot::PubPtr monitor = new PubCenterI;
        m_prxSub = adapter->addWithUUID(monitor)->ice_oneway();
        adapter->activate();
//        m_prxPub->Notify("pub","nihao");

    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void DlgMain::on_Btn_sub_clicked()
{
    IceStorm::TopicPrx topic;
    try
    {
        topic = m_topicManager->retrieve("Notify");
        IceStorm::QoS qos;
        topic->subscribeAndGetPublisher(qos, m_prxSub);
    }
    catch (const IceStorm::NoSuchTopic&)
    {
        qDebug() << "No such topic!";

    }
}

void DlgMain::on_Btn_session_clicked()
{
    QString m_data;
    std::vector<std::string> m_strList;
    try{
        m_prxSession->List(m_strList);
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
    for(int i = 0;i < m_strList.size();i++){
        std::string str = m_strList.at(i);
        m_data.append(str.c_str());
    }
    ui->lineEdit_session->setText(m_data);
}

void DlgMain::on_Btn_liveStart_clicked()
{
    try{
        m_prxLive->RemoteRecvStart();
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void DlgMain::on_Btn_liveStop_clicked()
{
    try{
        m_prxLive->RemoteRecvStop();
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}
